﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using CrankShaftAnalysisSystem.Devices;

namespace CrankShaftAnalysisSystem
{
    public partial class FormMT2571 : Form
    {
        public DistanceMT2571 MT2571;
        public WaveGrapher grapher;
        public int PointCnt { get; private set; }
        public List<double> XValues;
        public List<double> YValues;
        public double InstantDistance;
        public const string Title = "MT2571";
        public bool TestRunning;
        public bool BusyRunning;
        public delegate void DeleVoidType();
        public DeleVoidType RefreshUi;
        public FormMT2571()
        {
            InitializeComponent();
            MT2571 = DistanceMT2571.Instance;
            grapher = new WaveGrapher(ref Zed);
            XValues = new List<double>();
            YValues = new List<double>();
            grapher.InitAxis(new string[1] { Title });
            RefreshUi = new DeleVoidType(AddOnePoint);
        }

        private void btnInit_Click(object sender, EventArgs e)
        {
            tmLoop.Start();
        }

        private void tmLoop_Tick(object sender, EventArgs e)
        {
            lbState.BackColor = MT2571.Device.RefIndexFound ? Color.Green : Color.Red;
            if (MT2571.Initialised)
            {
                InstantDistance = MT2571.GetDistance();
                lbDistance.Text = InstantDistance.ToString("F2") + "um";
            }
        }

        private void FormMT2571_FormClosing(object sender, FormClosingEventArgs e)
        {
            Hide();
            e.Cancel = true;
        }

        private void btnAddPoint_Click(object sender, EventArgs e)
        {
            AddOnePoint();
        }

        private void AddOnePoint()
        {
            PointCnt++;
            XValues.Add(PointCnt);
            YValues.Add(InstantDistance);
            grapher.ShowCurve(Title, XValues.ToArray(), YValues.ToArray());
        }

        private void FormMT2571_Load(object sender, EventArgs e)
        {

        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            PointCnt = 0;
            XValues.Clear();
            YValues.Clear();
            grapher.ShowCurve(Title, XValues.ToArray(), YValues.ToArray());
        }

        private void btnLoopTest_Click(object sender, EventArgs e)
        {
            if(TestRunning || BusyRunning)
            {
                return;
            }
            Task.Run(() =>
            {
                BusyRunning = true;
                TestRunning = true;
                tmLoop.Start();
                string file_name = $"MT2571Cycle-{DateTime.Now.ToString("yy-MM-dd-HH-mm-ss")}.csv";
                while (true)
                {
                    MeasureSystem.Instance.DrawBackMT2571().Wait();
                    Thread.Sleep(1000);
                    MeasureSystem.Instance.StretchOutMT2571().Wait();
                    Thread.Sleep(1000);
                    using (var file = new StreamWriter(file_name,append:true))
                    {                        
                        var item = $"{PointCnt},{InstantDistance}";
                        file.WriteLine(item);
                    }
                    Invoke(RefreshUi);
                    if (!TestRunning)
                        break;
                }
                BusyRunning = false;
            });
        }

        private void btnStopLoop_Click(object sender, EventArgs e)
        {
            TestRunning = false;
        }
    }
}
